﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using NLog;
using FPC.Offer.Common;
namespace FPC.Offer.UI
{
    public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
        }
        private static Logger logger = LogManager.GetCurrentClassLogger();
        /// <summary>
        /// 在出现未处理的错误时运行的代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Application_Error(object sender, EventArgs e)
        {
            //记录错误日志
            Exception ex = Server.GetLastError();
            if (ex != null)
            {
                string errorMsg = ex.Message;
                if (ex.InnerException != null)
                {
                    errorMsg = ex.InnerException.Message;
                }
                //写入错误日             
                 logger.Error("系统拦截异常："  + errorMsg, ex);
                //如果是 HttpException 异常 不处理, 显示403 404 页面
                if (ex is HttpException)
                {
                    HttpException httpEx = ex as HttpException;
                    if (httpEx.GetHttpCode() == 403 || httpEx.GetHttpCode() == 404)
                        return;
                }
                if (HttpContext.Current.Server.GetLastError() is HttpRequestValidationException)
                {
                    HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.ToString());
                    HttpContext.Current.Server.ClearError();
                }
            }
        }
    }
}